package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.dto.PageDto;
import com.itheima.ydd.entity.YddUser;
import com.itheima.ydd.exception.ConsumerException;
import com.itheima.ydd.mapper.YddUserMapper;
import com.itheima.ydd.service.YddUserService;
import netscape.security.UserTarget;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Map;

/**
 * @author shiqiang
 * @version 1.0
 * @date 2022/4/9 16:33
 */
@Service
public class YddUserServiceImpl extends ServiceImpl<YddUserMapper, YddUser> implements YddUserService {
    /**
     * 根据id查询用户详情
     * @param id
     * @return
     */
    @Override
    public YddUser findById(String id) {
        return getById(Integer.parseInt(id));
    }

    /**
     * 分页查询员工列表(带有条件)
     * @param sex
     * @param username
     * @param mobile
     * @param page
     * @param limit
     * @return
     */
    @Override
    public PageDto<YddUser> getPage(String sex, String username, String mobile, String page, String limit) {
        //设置分页参数
        PageDto<YddUser> iPage=new PageDto<>(Integer.parseInt(page),Integer.parseInt(limit));
        LambdaQueryWrapper<YddUser> wrapper=new LambdaQueryWrapper<>();
        if (StringUtils.hasText(sex)){
            wrapper.eq(YddUser::getSex,Integer.parseInt(sex));
        }
        //构造用户名条件构造器
        wrapper.like(StringUtils.hasText(username),YddUser::getUsername,username);
        //构造手机号条件构造器
        wrapper.like(StringUtils.hasText(mobile),YddUser::getMobile,mobile);
        return page(iPage, wrapper);
    }

    /**
     * 添加用户
     * @param user
     */
    @Override
    public void add(@RequestBody YddUser user) {
        //设置YddUser对象
        //对password进行MD5加密
        String md5Pwd = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        LambdaQueryWrapper<YddUser> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(!ObjectUtils.isEmpty(user.getUsername()), YddUser::getUsername, user.getUsername());
        YddUser yddUser = getOne(wrapper);
        if (!ObjectUtils.isEmpty(yddUser)){
            //用户名存在
            throw new ConsumerException("用户名已存在");
        }
        user.setPassword(md5Pwd);
        this.save(user);
    }

    /**
     * 用户删除
     * @param id
     */
    @Override
    public void delete(String id) {
        this.removeById(Integer.parseInt(id));
    }

    /**
     * 修改用户
     * @param user
     */
    @Override
    public void edit(YddUser user) {
        //对password进行MD5加密
        String md5Pwd = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(md5Pwd);
        LambdaQueryWrapper<YddUser> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(!ObjectUtils.isEmpty(user.getUsername()),YddUser::getUsername,user.getUsername());
        YddUser yddUser = getOne(wrapper);
        if (!ObjectUtils.isEmpty(yddUser)&&yddUser.getId()!=user.getId()){
            throw new ConsumerException("用户名已存在");
        }
        this.updateById(user);
    }
}
